﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebService_Newstore.DAO
{
    public class BpDAO:BaseDAO
    {
        public int numero;
        public DateTime date;
        public int statut;
        public int numeroCommande;
        public int type;               //type de préparation: palette ou picking
        public int idUser;

        public BpDAO() { }

        public BpDAO(int id, DateTime date, int statut, int idCmd, int type, int idUser)
        {
            this.numero = id;
            this.date = date;
            this.statut = statut;
            this.numeroCommande = idCmd;
            this.type = type;
            this.idUser = idUser;
        }
        public BpDAO(DateTime date, int statut, int idCmd, int type, int idUser)
        {
            this.date = date;
            this.statut = statut;
            this.numeroCommande = idCmd;
            this.type = type;
            this.idUser = idUser;
        }

        //constructeur pour chargement d'une entete par l'ID
        public BpDAO(int idBp)
        {
            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var userQuery = from bp in newStoreContext.bps
                                    where bp.id_bp == idBp
                                    select bp;

                    if (userQuery.LongCount() == 0)
                    {
                        //On instancie un DAO vide pour faire remonter l'erreur au niveau du controller
                        this.numero = 0;
                    }

                    foreach (var ligne in userQuery)
                    {
                        //On instancie le BP
                        this.numero = ligne.id_bp;
                        this.numeroCommande = (int)ligne.num_cmd;
                        this.date = (DateTime)ligne.dt_BP;
                        this.type = (int)ligne.typBP;
                        this.idUser = (int)ligne.id_util;
                        this.statut = (int)ligne.statut_bp;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                }
            }
        }

        public static List<BpDAO> FindBpDAOByCmd(int numCmd)
        {
            List<BpDAO> listeBP = new List<BpDAO>();

            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var query = from bp in newStoreContext.bps
                                where bp.num_cmd == numCmd
                                select bp;

                    if (query.LongCount() == 0)
                        throw new Exception("Aucun BP ne correspond à ce numéro de commande!!");

                    foreach (var bp in query)
                    {

                        //On instancie les BP DAO
                        BpDAO bpToAdd = new BpDAO();
                        bpToAdd.numero = bp.id_bp;
                        bpToAdd.numeroCommande = (int)bp.num_cmd;
                        bpToAdd.date = (DateTime)bp.dt_BP;
                        bpToAdd.statut = (int)bp.statut_bp;
                        bpToAdd.type = (int)bp.typBP;
                        bpToAdd.idUser = (int)bp.id_util;

                        listeBP.Add(bpToAdd);
                        
                    }

                    return listeBP;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                    return listeBP;
                }

            }
        }

        public static int LasInsertedIDBP()
        {
            using (newStoreContext = new newStoreEntities())
            {
                bp b = newStoreContext.bps.Where(x => x.id_bp > 0).OrderByDescending(x => x.id_bp).FirstOrDefault();
                int id = b.id_bp;
                return id;
            }
        }

        public static BpDAO SaveBP(BpDAO bpdao)
        {
            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    bp b = newStoreContext.bps.FirstOrDefault(i => i.id_bp == bpdao.numero);
                    if (b != null)
                    {
                        //Modification
                        b.dt_BP = bpdao.date;
                        b.statut_bp = bpdao.statut;
                        b.num_cmd = bpdao.numeroCommande;
                        b.typBP = bpdao.type;
                        b.id_util = bpdao.idUser;
                        newStoreContext.SaveChanges();
                        return bpdao;
                    }
                    else
                    {
                        //Insertion
                        bp bptocreate = new bp();
                        bptocreate.dt_BP = bpdao.date;
                        bptocreate.statut_bp = bpdao.statut;
                        bptocreate.num_cmd = bpdao.numeroCommande;
                        bptocreate.typBP = bpdao.type;
                        bptocreate.id_util = bpdao.idUser;
                        newStoreContext.bps.AddObject(bptocreate);
                        newStoreContext.SaveChanges();
                        bpdao.numero = LasInsertedIDBP();
                        return bpdao;
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.InnerException);
                    return bpdao;
                }
            }
        }

        public static int CountReadyBp(int idUser)
        {
            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var query = from bp in newStoreContext.bps
                                where bp.id_util == idUser && bp.statut_bp < 3
                                select bp;

                    return query.Count();

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                    return 0;
                }
            }
        }

        public static List<BpDAO> FindReadyBpByUser(int idUser)
        {
            List<BpDAO> listeBP = new List<BpDAO>();

            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var query = from bp in newStoreContext.bps
                                where bp.id_util == idUser && bp.statut_bp < 3
                                select bp;

                    foreach (var bp in query)
                    {

                        //On instancie les BP DAO
                        BpDAO bpToAdd = new BpDAO();
                        bpToAdd.numero = bp.id_bp;
                        bpToAdd.numeroCommande = (int)bp.num_cmd;
                        bpToAdd.date = (DateTime)bp.dt_BP;
                        bpToAdd.statut = (int)bp.statut_bp;
                        bpToAdd.type = (int)bp.typBP;
                        bpToAdd.idUser = (int)bp.id_util;

                        listeBP.Add(bpToAdd);

                    }

                    return listeBP;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                    return listeBP;
                }

            }
        }

        public static List<BpDAO> FindBPByDate(DateTime startDate, DateTime endDate)
        {
            List<BpDAO> listeBP = new List<BpDAO>();

            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var query = from bp in newStoreContext.bps
                                where bp.dt_BP >= startDate && bp.dt_BP <= endDate
                                select bp;

                    if (query.LongCount() == 0)
                        throw new Exception("Aucun BP ne correspond à ce numéro de commande!!");

                    foreach (var bp in query)
                    {

                        //On instancie les BP DAO
                        BpDAO bpToAdd = new BpDAO();
                        bpToAdd.numero = bp.id_bp;
                        bpToAdd.numeroCommande = (int)bp.num_cmd;
                        bpToAdd.date = (DateTime)bp.dt_BP;
                        bpToAdd.statut = (int)bp.statut_bp;
                        bpToAdd.type = (int)bp.typBP;
                        bpToAdd.idUser = (int)bp.id_util;

                        listeBP.Add(bpToAdd);

                    }

                    return listeBP;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                    return listeBP;
                }
            }
        }

    }
}